package com.zt.teacher.service.impl;

import com.zt.common.Do.OriginStudentDO;
import com.zt.common.common.ApiException;
import com.zt.common.common.CodeMsg;
import com.zt.common.pojo.ActiveClass;
import com.zt.common.pojo.ClassSelection;
import com.zt.common.pojo.StudentVx;
import com.zt.common.pojo.netty.Sender;
import com.zt.common.pojo.origin.OriginStudent;
import com.zt.common.utils.ClassUtil;
import com.zt.teacher.dao.ClassSelectionDao;
import com.zt.teacher.dto.StudentInfoDTO;
import com.zt.teacher.service.ClassSelectionService;
import com.zt.teacher.service.OriginStudentService;
import com.zt.teacher.service.StudentVxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description:
 * author:zt
 * date:2021-12-26
 */
@Service
@Transactional
public class ClassSelectionServiceImpl implements ClassSelectionService {

    @Autowired
    private ClassSelectionDao classSelectionDao;
    @Autowired
    private OriginStudentService originStudentService;
    @Autowired
    private StudentVxService studentVxService;

    /**
     * 默认必修课 老师开课即将 originClass中的学生加入 该课堂
     * @param activeClass 课堂id和originClass id
     */
    @Override
    public void insertCompulsory(ActiveClass activeClass,List<Integer> originClassIds){
        classSelectionDao.insertCompulsory(activeClass.getId(),originClassIds);
    }

    /**
     * @param classId 课堂id
     * @return 选择该课堂的学生id
     */
    @Override
    public List<String> getStuIdByClassId(Integer classId) {
        return classSelectionDao.getStuIdByClassId(classId);
    }

    @Override
    public void delete(List<String> studentIds, Integer classId) {
        classSelectionDao.delete(studentIds,classId);
    }

    /**
     * @param classId 班级id
     * @return 返回该班级中所有学生的 学号名字头像 身份信息
     */
    @Override
    public List<StudentInfoDTO> getStuInfoByClassId(int classId){
        List<StudentInfoDTO> res = new ArrayList<>();
        //获取该班级选择学生id以及评分情况
        List<ClassSelection> stuAndScore = classSelectionDao.getStuAndScoreByClassId(classId);
        List<String> studentIds = new ArrayList<>();

        //关联学生id和分数
        HashMap<String, Integer> scoreMap = new HashMap<>();
        for (ClassSelection classSelection : stuAndScore) {
            scoreMap.put(classSelection.getStudentId(),classSelection.getPoint());
            studentIds.add(classSelection.getStudentId());
        }
        //如果当前班级无人选择，则直接返回空数组
        if (stuAndScore == null || stuAndScore.size() == 0) return res;

        List<OriginStudentDO> originStudents = originStudentService.getStudentsById(studentIds);
        List<StudentVx> students = studentVxService.getStudentsByStuId(studentIds);

        //关联学生id和头像
        HashMap<String, String> studentIdToAvatar = new HashMap<>();
        students.forEach(s -> studentIdToAvatar.put(s.getStudentId(),s.getAvatar()));

        originStudents.forEach(s -> {
            StudentInfoDTO student = new StudentInfoDTO().setId(s.getStudentId()).setName(s.getStudentName()).setClassName(s.getClassName()).
                    setAvatar(studentIdToAvatar.getOrDefault(s.getStudentId(), null)).
                    setScore(scoreMap.get(s.getStudentId()));
            res.add(student);
        });

//        studentIds.forEach(id -> {
//            OriginStudent originStudent = originStudentService.getOriginStuById(id);
//            StudentVx studentVx = studentVxService.getStudentByStuId(id);
//            res.add(new Sender(id,originStudent.getStudentName(),studentVx.getAvatar()));
//        });

        return res;
    }

    @Override
    public void addScore(ClassSelection classSelection) {
        //获取当前学期
        String semester = ClassUtil.getSemester(0);
        //获取当前月份
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;

//        if (semester.endsWith("2")){
//            //表示当前为第二学期
//            if (month != 8) throw new ApiException(CodeMsg.NOT_SCORE_TIME);
//        } else {
//            //表示第一学期
//            if (month != 2) throw new ApiException(CodeMsg.NOT_SCORE_TIME);
//        }

        classSelectionDao.addScore(classSelection);
    }
}
